/******************************************************************************
//             INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 2000-2001 Intel Corporation. All Rights Reserved.
//
//  Description:
//      Intel(R) JPEG library for X platform 
//
********************************************************************************/
#include "_ijx.h"


/******************************************************************************
// Name:            ijAdjustMCUBufferFwd_1
// Description:     Adjust MCU buffer for 1 component
//
// Input Arguments:
//        pMCUSpec:  Points to IJMCUSPEC structure 
// Output Arguments:
//      pIJXBuffer:  Points to IJBUFFER structure
//                          
******************************************************************************/
void ijAdjustMCUBufferFwd_1 (IJMCUSPEC *pMCUSpec,
                             IJBUFFER  *pIJXBuffer)
{
    pIJXBuffer->paImgCoeffBufCur[0]+=pMCUSpec->naCoeffOffset[0];
}

/******************************************************************************
// Name:            ijAdjustMCUBufferFwd_3
// Description:     Adjust MCU buffer for 3 component
//
// Input Arguments:
//        pMCUSpec:  Points to IJMCUSPEC structure 
// Output Arguments:
//      pIJXBuffer:  Points to IJBUFFER structure
//                          
//                  
******************************************************************************/
void ijAdjustMCUBufferFwd_3 (IJMCUSPEC *pMCUSpec,
                             IJBUFFER  *pIJXBuffer)
{
    pIJXBuffer->paImgCoeffBufCur[0]+=pMCUSpec->naCoeffOffset[0];
    pIJXBuffer->paImgCoeffBufCur[1]+=pMCUSpec->naCoeffOffset[1];
    pIJXBuffer->paImgCoeffBufCur[2]+=pMCUSpec->naCoeffOffset[2];
}

/******************************************************************************
// Name:            ijWriteHead
// Description:     Write JPEG stream head.
// Input Arguments:
//        pComment: Points to the buffer that defined comments for main code stream
//     nCommentLen: Specifies the comment buffer length. 
//         pStream: Points to IJBITTREAMSTATE data stream.
//    pEncodeState: Points to IJENCODESTATE data stream.
//                  
******************************************************************************/
IJXRESULT ijWriteHead( char *pComment,
                       int  nCommentLen,
                       IJBITTREAMSTATE *pStream,
                       IJENCODESTATE *pEncodeState)
{
    IJXRESULT rtCode;
    IIPIMAGESPEC *pImageSpec;

    /* Write SOI */
    rtCode=ijWrite_Marker(IJX_MARKER_SOI, pStream);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    /* APP0 for general */
    pImageSpec=&(pEncodeState->imageSpec);
    rtCode=ijWrite_APP0_JFIF((Ipp8u)pImageSpec->nDensityUnit,
                             pImageSpec->nXDensity,
                             pImageSpec->nYDensity,
                             _IJX_MAKEVERSION_16U(1,2),
                             NULL,
                             pStream);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    /* APP0 for motion */
    if (IJX_MODE_MOTION_MASK&pEncodeState->nJPEGCodeMode) {
        rtCode=ijWrite_APP0_IJXM( pEncodeState->nJPEGCodeMode,
                                  &(pEncodeState->imageSpec),
                                  &(pEncodeState->waveSpec),
                                  &(pEncodeState->motionSpec),  
                                  pStream );
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }
    }
    
    /* APP0 for com */

    if ((0<nCommentLen)&&(NULL!=pComment)){
        rtCode=ijWrite_COM(pComment, nCommentLen, pStream);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }
    }

    if (0<pEncodeState->nRstInterval) {
        ijWrite_DRI(pEncodeState->nRstInterval, pStream);
    }

    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijAdjustRestartIntervalFwd
// Description:     Process restart interval
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pEncodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//                   NONE       
// Returns:          
//                   NONE
//                     
//                  
******************************************************************************/
void ijAdjustRestartIntervalFwd(IJBITTREAMSTATE *pStream,
                                IJENCODESTATE *pEncodeState)
{
    pEncodeState->dcPred[0]=0;
    pEncodeState->dcPred[1]=0;
    pEncodeState->dcPred[2]=0;
    pEncodeState->dcPred[3]=0;

    ijWrite_RST(pEncodeState->counter.nRSTCounter, pStream);
    pEncodeState->counter.nDRICounter=pEncodeState->nRstInterval;
    pEncodeState->counter.nRSTCounter=(pEncodeState->counter.nRSTCounter+1)&7;
}

/******************************************************************************
// Name:            ijCalcBufferSize
// Description:     Calculate internal buffer length for compressing
// Input Arguments:
//   nJPEGCodeMode: Specifies JPEG Mode.
//        pMCUSpec: Points to IJMCUSPEC data structure.
// Output Arguments:
//       pIJXBuffer:Points to IJBUFFER data structure.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_ERROR
//                     
//                  
******************************************************************************/
IJXRESULT ijCalcEncBufferSize(int         nJPEGCodeMode,
                              IJMCUSPEC   *pMCUSpec,
                              IJBUFFER    *pIJXBuffer)
{
    int nMCUDataTempBufLen, nFreqBufLen, nBlockCount;

    pIJXBuffer->paMCUCoeffBufCur[0]=pIJXBuffer->paMCUCoeffBuf[0]=NULL;                    
    pIJXBuffer->paMCUCoeffBufCur[1]=pIJXBuffer->paMCUCoeffBuf[1]=NULL;
    pIJXBuffer->paMCUCoeffBufCur[2]=pIJXBuffer->paMCUCoeffBuf[2]=NULL;
    pIJXBuffer->paMCUCoeffBufCur[3]=pIJXBuffer->paMCUCoeffBuf[3]=NULL;
    pIJXBuffer->paImgCoeffBufCur[0]=pIJXBuffer->paImgCoeffBuf[0]=NULL;                    
    pIJXBuffer->paImgCoeffBufCur[1]=pIJXBuffer->paImgCoeffBuf[1]=NULL;
    pIJXBuffer->paImgCoeffBufCur[2]=pIJXBuffer->paImgCoeffBuf[2]=NULL;
    pIJXBuffer->paImgCoeffBufCur[3]=pIJXBuffer->paImgCoeffBuf[3]=NULL;
    pIJXBuffer->nCoeffBufLen=0;

    pIJXBuffer->pAuxFreqBuf=NULL;
    pIJXBuffer->pAuxMCUDataTempBuf=NULL;
    pIJXBuffer->nAuxBufLen=0;

    pIJXBuffer->nMaxBufLen=0;

    nFreqBufLen=0;

    if ((0<pMCUSpec->nMCUPadWidth)||(0<pMCUSpec->nMCUPadHeight)) {
        nMCUDataTempBufLen=(pMCUSpec->nImageMCUStep)*pMCUSpec->nMCUHeight;
    } else {
        nMCUDataTempBufLen=0;
    }

    nBlockCount=0;

    switch (nJPEGCodeMode) {
        case IJX_MODE_BASELINE:
            pIJXBuffer->nFlag=IJX_JPEGBUFMODE_MCUORIMGCOEFF; //One MCU Coeff Buffer only
            nBlockCount=pMCUSpec->nBlksInMCU;
            break;
        case (IJX_MODE_BASELINE|IJX_MODE_MOTION_INDIV):
            pIJXBuffer->nFlag=IJX_JPEGBUFMODE_MCUORIMGCOEFF; //One MCU Coeff Buffer only
            nBlockCount=pMCUSpec->nBlksInMCU;
            break;

        case (IJX_MODE_BASELINE|IJX_MODE_MOTION_DIFF):
        case (IJX_MODE_BASELINE|IJX_MODE_MOTION_BRIEF):
            pIJXBuffer->nFlag=IJX_JPEGBUFMODE_MCUANDIMGCOEFF; //One MCU Coeff Buffer and One Img Coeff Buffer
            nBlockCount=(pMCUSpec->nTotalMCUCount+1)*pMCUSpec->nBlksInMCU;
            break;

///////////////////////////////////////
#ifdef _BASELINE_A
        case IJX_MODE_BASELINE_A:
            pIJXBuffer->nFlag=IJX_JPEGBUFMODE_MCUORIMGCOEFF; //One MCU Coeff Buffer only

            nBlockCount=(pMCUSpec->nTotalMCUCount)*pMCUSpec->nBlksInMCU;

            if (1<nNumComp) {
                nFreqBufLen=(((257+7)*sizeof(int))<<2);
            } else {
                nFreqBufLen=(((257+7)*sizeof(int))<<1);
            }

            break;
        case (IJX_MODE_BASELINE_A|IJX_MODE_MOTION_INDIV):
            pIJXBuffer->nFlag=IJX_JPEGBUFMODE_MCUIMGCOEFFSHARE; //One MCU Coeff Buffer shares One Img Coeff Buffer
            nBlockCount=(pMCUSpec->nTotalMCUCount)*pMCUSpec->nBlksInMCU;

            if (1<nNumComp) {
                nFreqBufLen=(((257+7)*sizeof(int))<<2);
            } else {
                nFreqBufLen=(((257+7)*sizeof(int))<<1);
            }

            break;

        case (IJX_MODE_BASELINE_A|IJX_MODE_MOTION_DIFF):
        case (IJX_MODE_BASELINE_A|IJX_MODE_MOTION_BRIEF):
            pIJXBuffer->nFlag=IJX_JPEGBUFMODE_MCUANDIMGCOEFF; //One MCU Coeff Buffer and One Img Coeff Buffer
            nBlockCount=(pMCUSpec->nTotalMCUCount+1)*pMCUSpec->nBlksInMCU;
            if (1<nNumComp) {
                nFreqBufLen=(((257+7)*sizeof(int))*4);
            } else {
                nFreqBufLen=(((257+7)*sizeof(int))*2);
            }

            break;
#endif
///////////////////////////////////////

        case IJX_MODE_PROGRESSIVE:
            pIJXBuffer->nFlag=IJX_JPEGBUFMODE_MCUORIMGCOEFF; //One MCU Coeff Buffer only
            nBlockCount=(pMCUSpec->nTotalMCUCount)*pMCUSpec->nBlksInMCU;
            nFreqBufLen =(((257+7)*sizeof(int))*2);
            break;

        case (IJX_MODE_PROGRESSIVE|IJX_MODE_MOTION_INDIV):
            pIJXBuffer->nFlag=IJX_JPEGBUFMODE_MCUIMGCOEFFSHARE; //One MCU Coeff Buffer shares One Img Coeff Buffer
            nBlockCount=(pMCUSpec->nTotalMCUCount)*pMCUSpec->nBlksInMCU;
            nFreqBufLen =(((257+7)*sizeof(int))*2);
            break;
        case (IJX_MODE_PROGRESSIVE|IJX_MODE_MOTION_DIFF):
        case (IJX_MODE_PROGRESSIVE|IJX_MODE_MOTION_BRIEF):
            pIJXBuffer->nFlag=IJX_JPEGBUFMODE_MCUANDIMGCOEFF; //One MCU Coeff Buffer and One Img Coeff Buffer
            nBlockCount=(pMCUSpec->nTotalMCUCount+1)*pMCUSpec->nBlksInMCU;
            nFreqBufLen  =(((257+7)*sizeof(int))<<1);
            break;

        case IJX_MODE_MOTION_INDIV:
            pIJXBuffer->nFlag=IJX_JPEGBUFMODE_MCUORIMGCOEFF; //One MCU Coeff Buffer only
            nBlockCount=pMCUSpec->nBlksInMCU;
            break;
        case IJX_MODE_MOTION_DIFF:
        case IJX_MODE_MOTION_BRIEF:
            pIJXBuffer->nFlag=IJX_JPEGBUFMODE_MCUANDIMGCOEFF; //One MCU Coeff Buffer and One Img Coeff Buffer
            nBlockCount=(pMCUSpec->nTotalMCUCount+1)*pMCUSpec->nBlksInMCU;
            break;
        default:
            return IJX_RTCODE_ERROR;
    }

    pIJXBuffer->nCoeffBufLen  = (nBlockCount<<6)*sizeof(Ipp16s)+31;

    pIJXBuffer->nAuxBufLen    = _IJX_MAX(nMCUDataTempBufLen,nFreqBufLen);
    if (pIJXBuffer->nAuxBufLen) pIJXBuffer->nAuxBufLen+=31;

    pIJXBuffer->nMaxBufLen    = (pIJXBuffer->nCoeffBufLen)+(pIJXBuffer->nAuxBufLen);

    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijCompress_BaseLine_L
// Description:     Compress the image in JPEG Baseline mode.
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pEncodeState: Identifies IJDECODESTATE strcuture.
//                     
//                  
******************************************************************************/
IJXRESULT ijCompress_BaseLine_L(IJBITTREAMSTATE *pStream,
                                IJENCODESTATE *pEncodeState)
{
    IJXRESULT rtStatus, rtCode;
/*
    rtCode=ijInitFrameSpecFwd(IJX_MODE_BASELINE, pEncodeState);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    rtCode=ijWrite_SOF(&(pEncodeState->frameSpec),
                       pEncodeState->compSpec,
                       pStream);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }
*/
    rtStatus=ijInitScanSpecFwd(pEncodeState);
    if (!IJX_SUCCEEDED(rtStatus)){
        return rtStatus;
    }

    rtCode=ijWrite_SOS(&(pEncodeState->scanSpec),pStream);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    rtCode=ijEncodeScanData_Baseline(pStream,pEncodeState);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    if (0!=(IJX_MODE_MOTION_MASK&pEncodeState->nJPEGCodeMode)){
        pEncodeState->nStatus|=IJX_STATUS_PROCESS;
        pEncodeState->nJPEGCodeMode&=~IJX_MODE_STILL_MASK;
        if (IJX_MODE_MOTION_DIFF&pEncodeState->nJPEGCodeMode) {
            pEncodeState->procSpec.funcCompress=(PFUNCODEC)ijCompress_Motion_Diff;
        } else if (IJX_MODE_MOTION_BRIEF&pEncodeState->nJPEGCodeMode) {
            pEncodeState->procSpec.funcCompress=(PFUNCODEC)ijCompress_Motion_Brief;
        } else {
            pEncodeState->procSpec.funcCompress=(PFUNCODEC)ijCompress_Motion_Indiv;
        }
        
        rtCode=ijWrite_Marker(IJX_MARKER_EOI, pStream);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }
        rtStatus=IJX_RTCODE_S_CONTINUE;
    }

    return rtStatus;
}

/******************************************************************************
// Name:            ijCompress_BaseLine_H
// Description:     Not supported in this version
// Input Arguments:
// Output Arguments:
//                   NONE       
// Returns:          
//                   NONE
//                     
//                  
******************************************************************************/

///////////////////////////////////////
#ifdef _BASELINE_A
IJXRESULT ijCompress_BaseLine_H(IJBITTREAMSTATE *pStream,
                                IJENCODESTATE *pEncodeState)
{
    int *paFreq[4];
    IJXRESULT rtStatus, rtCode;
    int *pAUXBuffer=(int *)IIP_ALIGN_32B(pIJXBuffer->pAUXBuffer);

    //DC Luminance;
    paFreq[0]=pAUXBuffer;
    //AC Luminance;
    paFreq[1]=&pAUXBuffer[264];

    if (1<pEncodeState->imageSpec.nNumComponent) {
        //DC Chrominance;
        paFreq[2]=&pAUXBuffer[528];
        //AC Chrominance;
        paFreq[3]=&pAUXBuffer[792];
    }
/*
    rtCode=ijInitFrameSpecFwd(IJX_MODE_BASELINE_A, pEncodeState);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    rtCode=ijWrite_SOF(&(pEncodeState->frameSpec),
                       pEncodeState->compSpec,
                       pStream);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }
*/
    pImageData=(Ipp8u *) pIJXBuffer->paImageBufCur[0];
    nImageStep=pIJXBuffer->naImageStep[0];
    rtCode=ijIntiEncodeCoeff(pImageData, nImageStep, pIJXBuffer, pEncodeState);
    if (!IJX_SUCCEEDED(rtCode)) {
        return rtCode;
    }

    rtStatus=ijInitScanSpecFwd(pEncodeState);
    if (!IJX_SUCCEEDED(rtStatus)){
        return rtStatus;
    }

    rtCode=ijBuildScanHuffTables_ABLS(paFreq,
                                      pIJXBuffer,
                                      pStream,
                                      pEncodeState);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    rtCode=ijWrite_SOS(&(pEncodeState->scanSpec),pStream);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    rtCode=ijEncodeScanData_ABaseline(pIJXBuffer,pStream,pEncodeState);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    if (0!=(IJX_MODE_MOTION_MASK&pEncodeState->nJPEGCodeMode)){
        pEncodeState->nJPEGCodeMode&=~IJX_MODE_STILL_MASK;
        pEncodeState->nStatus|=IJX_STATUS_PROCESS;
        if (IJX_MODE_MOTION_DIFF&pEncodeState->nJPEGCodeMode) {
            pEncodeState->procSpec.funcCompress=(PFUNCODEC)ijCompress_Motion_Diff;
        } else if (IJX_MODE_MOTION_BRIEF&pEncodeState->nJPEGCodeMode) {
            pEncodeState->procSpec.funcCompress=(PFUNCODEC)ijCompress_Motion_Brief;
        } else {
            pEncodeState->procSpec.funcCompress=(PFUNCODEC)ijCompress_Motion_Indiv;
        }
        rtCode=ijWrite_Marker(IJX_MARKER_EOI, pStream);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }
        rtStatus=IJX_RTCODE_S_CONTINUE;
    }

    return rtStatus;
}
#endif
///////////////////////////////////////

/******************************************************************************
// Name:            ijCompress_Progressive
// Description:     Compress image in JPEG progressive mode.
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pEncodeState: Identifies IJDECODESTATE strcuture.
//                     
//                  
******************************************************************************/
#if IPPJPVER!=1

IJXRESULT ijCompress_Progressive(IJBITTREAMSTATE *pStream,
                                 IJENCODESTATE *pEncodeState)
{
    int *paFreq[2];
    IJXRESULT rtStatus, rtCode;
    int *pAUXBuffer=pEncodeState->bufSpec.pAuxFreqBuf;

    //DC Luminance;
    paFreq[0]=pAUXBuffer;
    //AC Luminance;
    paFreq[1]=&pAUXBuffer[264];

  /*
    rtCode=ijInitFrameSpecFwd(IJX_MODE_PROGRESSIVE, pEncodeState);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    rtCode=ijWrite_SOF(&(pEncodeState->frameSpec),
                       pEncodeState->compSpec,
                       pStream);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }
 */
    rtCode=ijIntiEncodeCoeff(pEncodeState);

    if (!IJX_SUCCEEDED(rtCode)) {
        return rtCode;
    }
 
    do {
        rtStatus=ijInitScanSpecFwd(pEncodeState);
        if (!IJX_SUCCEEDED(rtStatus)){
            return rtStatus;
        }

        rtCode=ijBuildScanHuffTables_P(paFreq,
                                       pStream,
                                       pEncodeState);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }

        rtCode=ijWrite_SOS(&(pEncodeState->scanSpec),pStream);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }

        rtCode=ijEncodeScanData_Progressive(pStream,pEncodeState);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }

    } while(IJX_RTCODE_S_CONTINUE==rtStatus);

    if (0!=(IJX_MODE_MOTION_MASK&pEncodeState->nJPEGCodeMode)){
        pEncodeState->nJPEGCodeMode&=~IJX_MODE_STILL_MASK;
        pEncodeState->nStatus|=IJX_STATUS_PROCESS;
        if (IJX_MODE_MOTION_DIFF&pEncodeState->nJPEGCodeMode) {
            pEncodeState->procSpec.funcCompress=(PFUNCODEC)ijCompress_Motion_Diff;
        } else if (IJX_MODE_MOTION_BRIEF&pEncodeState->nJPEGCodeMode) {
            pEncodeState->procSpec.funcCompress=(PFUNCODEC)ijCompress_Motion_Brief;
        } else {
            pEncodeState->procSpec.funcCompress=(PFUNCODEC)ijCompress_Motion_Indiv;
        }
        rtCode=ijWrite_Marker(IJX_MARKER_EOI, pStream);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }
        rtStatus=IJX_RTCODE_S_CONTINUE;
    }

    return rtStatus;
}

#endif
/******************************************************************************
// Name:            ijCompress_Motion_Indiv
// Description:     Compressed the image in Motion JPEG mode
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pEncodeState: Identifies IJDECODESTATE strcuture.
//                     
//                  
******************************************************************************/
IJXRESULT ijCompress_Motion_Indiv(IJBITTREAMSTATE *pStream,
                                  IJENCODESTATE *pEncodeState)
{
    IJXRESULT rtCode;

    /* Write SOFV */
    rtCode=ijWrite_Marker(IJX_MARKER_SOFV, pStream);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    rtCode=ijEncodeScanData_Motion(pStream,pEncodeState);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    ijAdjustStream(pStream);

    return IJX_RTCODE_S_CONTINUE;
}

/******************************************************************************
// Name:            ijCompress_Motion_Diff
// Description:     Not supported in this version 
// Input Arguments:
// Output Arguments:
//                   NONE       
// Returns:          
//                   NONE
//                     
//                  
******************************************************************************/
///////////////////////////////////////
#ifdef _MOTION_DIFF
IJXRESULT ijCompress_Motion_Diff(IJBITTREAMSTATE *pStream,
                                 IJENCODESTATE *pEncodeState)
{
    Ipp8u     *pImageData;
    int       nImageStep;
    IJXRESULT rtStatus, rtCode;
/*
    if (0==(IJX_STATUS_DQHT&pEncodeState->nStatus)) {
        rtCode=ijBuildEncodeQuantTables(pEncodeState->imageSpec.nNumComponent,
                                        NULL, pStream, pEncodeState);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }

        ijBuildDecodeQuantTables(pEncodeState->imageSpec.nNumComponent, pEncodeState);

        rtCode=ijBuildScanHuffTables_BLS(pStream, pEncodeState);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }
        pEncodeState->nStatus|=IJX_STATUS_DQHT;
    }
*/
    if (0==(IJX_STATUS_FRAME&pEncodeState->nStatus)) {
        rtCode=ijInitFrameSpecFwd(IJX_MODE_MOTION_DIFF, pEncodeState);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }

        rtCode=ijWrite_SOF(&(pEncodeState->frameSpec),
                           pEncodeState->compSpec,
                           pStream);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }
        pEncodeState->nStatus|=IJX_STATUS_FRAME;
    }

    rtStatus=ijInitScanSpecFwd(pEncodeState);
    if (!IJX_SUCCEEDED(rtStatus)){
        return rtStatus;
    }

    rtCode=ijWrite_SOS(&(pEncodeState->scanSpec),pStream);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    pImageData=(Ipp8u *) pIJXBuffer->paImageBufCur[0];
    nImageStep=pIJXBuffer->naImageStep[0];
    rtCode=ijEncodeScanData_Motion_Diff(pImageData, nImageStep, pIJXBuffer, 
                                        pStream,pEncodeState);

    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    if (pEncodeState->procSpec.funcMotionDiffEncoder!=pEncodeState->procSpec.funcMotionDiffRefine) {
        pEncodeState->counter.nBaseMotionFrameBits=(pEncodeState->counter.nCurMotionFrameBits*100)>>7;
        pEncodeState->procSpec.funcMotionDiffEncoder=pEncodeState->procSpec.funcMotionDiffRefine;
    } else {
        int nCurFrameBits=pEncodeState->counter.nCurMotionFrameBits;
        int nBaseFrameBits=pEncodeState->counter.nBaseMotionFrameBits;
        if (nBaseFrameBits<nCurFrameBits) {
            pEncodeState->nStatus&=(~IJX_STATUS_FRAME);
        }
    }

    ijAdjustStream(pStream);

    return IJX_RTCODE_S_CONTINUE;
}
#endif
///////////////////////////////////////

/******************************************************************************
// Name:            ijCompress_Motion_Brief
// Description:      Not supported in this version
// Input Arguments:
// Output Arguments:
//                   NONE       
// Returns:          
//                   NONE
//                     
//                  
******************************************************************************/
///////////////////////////////////////
#ifdef _MOTION_BRIEF
IJXRESULT ijCompress_Motion_Brief(IJBITTREAMSTATE *pStream,
                                  IJENCODESTATE *pEncodeState)
{
    Ipp8u     *pImageData;
    int       nImageStep;
    int       nCurBitsLen;
    IJXRESULT rtStatus, rtCode;
/*
    if (0==(IJX_STATUS_DQHT&pEncodeState->nStatus)) {
        rtCode=ijBuildEncodeQuantTables(pEncodeState->imageSpec.nNumComponent,
                                        NULL, pStream, pEncodeState);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }

        ijBuildDecodeQuantTables(pEncodeState->imageSpec.nNumComponent, pEncodeState);

        rtCode=ijBuildScanHuffTables_BLS(pStream, pEncodeState);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }
        pEncodeState->nStatus|=IJX_STATUS_DQHT;
    }
*/
    nCurBitsLen=pStream->nUsedBufBitsLen;

    if (0==(IJX_STATUS_FRAME&pEncodeState->nStatus)) {
        rtCode=ijInitFrameSpecFwd(IJX_MODE_MOTION_BRIEF, pEncodeState);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }

        rtCode=ijWrite_SOF(&(pEncodeState->frameSpec),
                           pEncodeState->compSpec,
                           pStream);
        if (!IJX_SUCCEEDED(rtCode)){
            return rtCode;
        }
        pEncodeState->nStatus|=IJX_STATUS_FRAME;
    }

    rtStatus=ijInitScanSpecFwd(pEncodeState);
    if (!IJX_SUCCEEDED(rtStatus)){
        return rtStatus;
    }

    rtCode=ijWrite_SOS(&(pEncodeState->scanSpec),pStream);
    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    pImageData=(Ipp8u *) pIJXBuffer->paImageBufCur[0];
    nImageStep=pIJXBuffer->naImageStep[0];
    rtCode=ijEncodeScanData_Motion_Diff(pImageData, nImageStep, pIJXBuffer, 
                                        pStream,pEncodeState);

    if (!IJX_SUCCEEDED(rtCode)){
        return rtCode;
    }

    if (pEncodeState->procSpec.funcMotionDiffEncoder!=pEncodeState->procSpec.funcMotionDiffRefine) {
        pEncodeState->counter.nBaseMotionFrameBits=(pEncodeState->counter.nCurMotionFrameBits*100)>>7;
        pEncodeState->procSpec.funcMotionDiffEncoder=pEncodeState->procSpec.funcMotionDiffRefine;
    } else {
        int nCurFrameBits=pEncodeState->counter.nCurMotionFrameBits;
        int nBaseFrameBits=pEncodeState->counter.nBaseMotionFrameBits;
        if (nBaseFrameBits<nCurFrameBits) {
            pEncodeState->nStatus&=(~IJX_STATUS_FRAME);
        } else {
            pStream->nUsedBufBitsLen=nCurBitsLen;
        }
    }

    ijAdjustStream(pStream);

    return IJX_RTCODE_S_CONTINUE;
}

#endif
///////////////////////////////////////

/* EOF */
